<?php

/**
 * IndexController - Index
 *
 * @author Fernando Dias Oliveira <rodox17@gmail.com>
 * @package intranet
 * @subpackage controller
 */

class AdminController extends AppBaseController 
{
	public function init()
	{
		parent::init();		
		
		$this->_helper->layout->disableLayout();
	}
	
	public function indexAction()
	{
		$caf01 = new caf01();
		$caf03 = new caf03();
		$caf05 = new caf05();
    	$caf10 = new caf10();
    	$caf13 = new caf13();
		
		$this->view->usuarios = $caf01->getAll();
		$this->view->id       = $caf01->getUltimoId();
		
		$this->view->cat      = $caf03->getAll(1);
		$this->view->dropcat  = $caf03->getDropDrow();
		$this->view->prod     = $caf05->fetchAll("caf05_status = 1");
		$this->view->par      = $caf13->find(1)->current()->toArray();
		
		/**
		 * Drop down.
		 */
    	$this->view->func = $caf01->getDropDrow();
    	$this->view->tp   = $caf10->getDropDrow();
	}
	
	function desativarUsuarioAction()
	{
		$caf01 = new caf01();
		$id    = $this->_request->getPost("id");
		
		$caf01->update(array("caf01_status" => 0), "caf01_cod = $id");
		
	}
	
	function listarCategoriaAction()
	{
		$caf03 = new caf03();
		$q     = $this->_request->getPost("buscar_categoria");
		$rs    = $this->_request->getPost("rs_categoria");
		
		$this->view->cat = $caf03->fetchAll("$rs LIKE '%$q%'");
	}
	
	function listarUsuarioAction()
	{
		$caf01 = new caf01();
		$q     = $this->_request->getPost("buscar_usuario");
		$rs    = $this->_request->getPost("rs_usuario");
		
		$this->view->usuarios = $caf01->fetchAll("$rs LIKE '%$q%'");
	}
	
	function listarProdutoAction()
	{
		$caf05 = new caf05();
		$q     = $this->_request->getPost("buscar_produto");
		$rs    = $this->_request->getPost("rs_produto");
		
		$this->view->prod = $caf05->fetchAll("$rs LIKE '%$q%'");
	}
	
	function novoUsuarioAction()
	{
		$caf01 = new caf01();
		$data  = $this->_request->getPost();
		$cod   = $data["caf01_cod"];
		
		if(!$data["caf01_admin"]){
			$data["caf01_admin"] = 0;
		}
		
		unset($data["caf01_cod"]);
		
		$data["caf01_user"] = ucwords($data["caf01_user"]);
		
		if($cod){
			unset($data["caf01_password"]);
			$caf01->update($data, "caf01_cod = $cod");
		}
		else
			$caf01->save($data);
			
		exit;
	}
	
	function desativarCategoriaAction()
	{
		$caf03 = new caf03();
		$id    = $this->_request->getPost("id");
		
		$caf03->update(array("caf03_status" => 0), "caf03_cod = $id");
		
		exit;
		
	}
	
	function novoCategoriaAction()
	{
		$caf03 = new caf03();
		$data  = $this->_request->getPost();
		$cod   = $data["caf03_cod"];
		
		$data["caf03_status"] = 1;
		
		unset($data["caf03_cod"]);
		
		if($cod)
			$caf03->update($data, "caf03_cod = $cod");
		else
			$caf03->save($data);
			
		exit;
	}
	
	function desativarProdutoAction()
	{
		$caf05 = new caf05();
		$id    = $this->_request->getPost("id");
		
		$caf05->update(array("caf05_status" => 0), "caf05_cod = $id");
		
		exit;
		
	}
	
	function parametroAction(){
		$caf13=new caf13();
		$caf13->update($_POST,"caf13_cod=1");
		
		exit;
	}
	
	function novoProdutoAction()
	{
		$caf05 = new caf05();
		$data  = $this->_request->getPost();
		$cod   = $data["caf05_cod"];
		
		$data["caf05_status"]      = 1;
		$data["caf05_valor"]       = str_replace(",", '.', $data["caf05_valor"]);
		$data["caf05_valor_promo"] = str_replace(",", '.', $data["caf05_valor_promo"]);
		
		unset($data["caf05_cod"]);
		
		if($cod)
			$caf05->update($data, "caf05_cod = $cod");
		else
			$caf05->save($data);
			
		exit;
	}
}

?>